package com.jfsnpm.controller;



import java.util.ArrayList;
import java.util.List;

import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.RoseType;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.data.PieData;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Pie;
import com.github.abel533.echarts.series.Series;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.NoUrlPara;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfsnpm.authority.Authority;
import com.jfsnpm.common.AppSql;
import com.jfsnpm.plugin.bjui.Bjui;

public class IndexController extends Controller {
	// 默认入口无需拦截包括"/" "/Index" "/Index/index"
	@Before(NoUrlPara.class)
	@Clear
	public void index(){
		redirect("/Index/main");
	}
	/**
	 * 主页面
	 */
	public void main(){
		setAttr("username", getSessionAttr("username"));
		setAttr("userno", getSessionAttr("userno"));
		Authority auth = new Authority(this);
		setAttr("systemlist", auth.getSystem());
		setAttr("toptenlist", auth.getTopten());
	}
	/**
	 * 我的主页
	 */
	public void myhome(){
		
	}
	/**
* 菜单点击量统计
*/
	public void menucountmysql(){
		GsonOption option = new GsonOption();
		option.title().text("菜单点击量统计").textStyle().fontFamily("Verdana");
		option.toolbox().show(true).feature(
		//Tool.mark,
		Tool.dataView,
		new MagicType(Magic.line, Magic.bar),
		Tool.restore,
		Tool.saveAsImage);
		option.tooltip();
		option.legend("菜单点击量");
		option.yAxis(new ValueAxis());
		CategoryAxis xaxis = new CategoryAxis();
		List<String> x = new ArrayList<String>();
		List<Long> y = new ArrayList<Long>();
		Line line = new Line();
		List<Record> datas = Db.find(AppSql.getsql("Index.menucountmysql"));
		for(Record data:datas){
		x.add(data.get("logdate").toString());
		y.add(Long.parseLong(data.get("logcount").toString()));
		}
		xaxis.data(x.toArray());
		option.xAxis(xaxis);
		line.name("菜单点击量").data(y.toArray());
		option.series(line);
		//setAttr("option", option.toString());
		renderJson(option.toString());
	} 
	/**
	 * 菜单点击量统计
	 */
	public void menucount(){
		GsonOption option = new GsonOption();
		option.title().text("各部门使用量统计").textStyle().fontFamily("Verdana");
		option.toolbox().show(true).feature(
				 //Tool.mark,
				 Tool.dataView,
				 new MagicType(Magic.line, Magic.bar),
				 Tool.restore,
				 Tool.saveAsImage); 
		option.tooltip();
		List<Record> datas = Db.find(AppSql.getsql("Index.menucount"));
		//系列
		List<String> legends = new ArrayList<String>();
		String legendname="";
		option.legend().setX("center");
		option.legend().setY("bottom");
		
		//X轴
		CategoryAxis xaxis = new CategoryAxis();
		List<String> x = new ArrayList<String>();
		List<Record> dataxs = Db.find(AppSql.getsql("Index.menucountx"));
		for(Record datax:dataxs){
			x.add(datax.get("logdate").toString());
		}
		xaxis.data(x.toArray());
		option.xAxis(xaxis);
		//Y轴
		option.yAxis(new ValueAxis());
		//数据
		List<Series> lines = new ArrayList<Series>();
		List<Long> y = new ArrayList<Long>();
		Line line = new Line();
		legendname = "";
		for(Record data:datas){
			if("".equals(legendname)) legendname = data.get("deptname");
			if(legendname.equals(data.get("deptname"))){
				y.add(Long.parseLong(data.get("logcount").toString()));
			}else{
				legends.add(legendname);
				line.name(legendname).data(y.toArray());
				y.clear();
				lines.add(line);
				line = new Line();
				legendname = data.get("deptname");
			}
		}
		legends.add(legendname);
		line.name(legendname).data(y.toArray());
		lines.add(line);
		
		option.legend(legends.toArray());
		option.series(lines);
		renderJson(option.toString());
	}
	/**
	 * 菜单分类饼图
	 */
	public void menucountpie(){
		GsonOption option = new GsonOption();
		option.title().text("系统使用量分类图").textStyle().fontFamily("Verdana");
		MagicType magictype = new MagicType(Magic.pie);
		option.toolbox().show(true).feature(
				 //Tool.mark,
				 Tool.dataView,
				 magictype,
				 Tool.restore,
				 Tool.saveAsImage); 
		option.tooltip();
		option.calculable(true);
		List<Record> datas = Db.find(AppSql.getsql("Index.menucountpie"));
		List<String> legends = new ArrayList<String>();
		for(Record data:datas){
			legends.add(data.getStr("path"));
		}
		option.legend(legends.toArray());
		option.legend().setOrient(Orient.vertical);
		option.legend().setX("left");
		option.legend().setY("center");
		Pie pie = new Pie().name("系统使用量");
		List<PieData> piedatas = new ArrayList<PieData>();
		for(Record data:datas){
			piedatas.add(new PieData(data.getStr("path"), Long.parseLong(data.get("qty").toString())));
		}
		pie.data(piedatas.toArray());
		pie.roseType(RoseType.area);
		option.series(pie);
		//System.out.println(option.toString());
		renderJson(option.toString());
	}
	/**
	 * 发起流程列表
	 */
	public void process(){
		//流程list
		String userid = getSessionAttr("userid");
		String sql = AppSql.getsql("Index.process");
		Bjui.BjuiPage(sql, "name,display_Name", "version asc", this, userid);
	}
	/**
	 * 待办任务
	 */
	public void mytodo(){
		String userid = getSessionAttr("userid");
		Bjui.BjuiPage(AppSql.getsql("pm.mytodoSelectP"), AppSql.getsql("pm.mytodoFromWhereP"),
				"wf_task.display_Name,pm_ext_wf_order.orderTitle,pm_user.name", "wf_task.create_Time desc", this, userid);
	}
	/**
	 * 待办任务TOP15
	 */
	public void mytodoTopTen(){
		String userid = getSessionAttr("userid");
		Bjui.BjuiPage(AppSql.getsql("pm.mytodoSelectP"), AppSql.getsql("pm.mytodoFromWhereP"), "", "wf_task.create_Time desc", this, userid);
	}
	/**
	 * 历史流程
	 */
	public void myhistory(){
		String userid = getSessionAttr("userid");
		Bjui.BjuiPage(AppSql.getsql("pm.myhistory"), "wf_process.display_Name,pm_user.name,pm_ext_wf_order.orderTitle,wf_hist_order.order_State", this, userid);
	}
	/**
	 * 流程进度
	 */
	public void myrelease(){
		String userid = getSessionAttr("userid");
		Bjui.BjuiPage(AppSql.getsql("pm.myrelease"), "wf_process.display_Name,pm_user.name,pm_ext_wf_order.orderTitle", this, userid);
	}
}	
